import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;


public class Utilities {
	/**
	 * Grabs an image from a file name
	 * @param filename - the file to grab an image from
	 * @return
	 * @throws IOException 
	 */
	public static BufferedImage getImage(String filename){
		BufferedImage im;
		try {
			im = ImageIO.read(Utilities.class.getResource("/resources/" + filename));
			Image im2 = makeColorTransparent(im, Color.white);
			return imageToBufferedImage(im2);
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(0);
		}
		return null;
	}
	/**
	 * Turns a given color transparent inside of an image
	 * @param im - the image to be edited
	 * @param color - the color to be made transparent
	 * @return- the filtered image
	 */
	private static Image makeColorTransparent(BufferedImage im, final Color color) {
		ImageFilter filter = new RGBImageFilter() {
			public int markerRGB = color.getRGB() | 0xFF000000;
			public final int filterRGB(int x, int y, int rgb) {
				if ((rgb | 0xFF000000) == markerRGB) {
					return 0x00FFFFFF & rgb;
				} else {
					return rgb;
				}
			}
		};
		ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
		return Toolkit.getDefaultToolkit().createImage(ip);
	}

	private static BufferedImage imageToBufferedImage(final Image image)  { 
		final BufferedImage bufferedImage =  
				new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);  
		final Graphics2D g2 = bufferedImage.createGraphics();  
		g2.drawImage(image, 0, 0, null);  
		g2.dispose();  
		return bufferedImage;  
	}
	public static <T> T findSomething(String name, ArrayList<T> list){
		for(int i = 0; i < list.size(); i++){
			if(list.get(i) instanceof Equip){
				Equip temp = (Equip) list.get(i);
				if(temp.getName().equals(name))
					return list.get(i);
			}
			else if(list.get(i) instanceof Item){
				Item temp = (Item) list.get(i);
				if(temp.getName().equals(name))
					return list.get(i);
			}
			else if(list.get(i) instanceof Action){
				Action temp = (Action) list.get(i);
				if(temp.getName().equals(name))
					return list.get(i);
			}
		}
		return null;
	}
	public static <T> int findIndex(String name, ArrayList<T> list){
		for(int i = 0; i < list.size(); i++){
			if(list.get(i) instanceof Equip){
				Equip temp = (Equip) list.get(i);
				if(temp.getName().equals(name))
					return i;
			}
			else if(list.get(i) instanceof Item){
				Item temp = (Item) list.get(i);
				if(temp.getName().equals(name))
					return i;
			}
			else if(list.get(i) instanceof Action){
				Action temp = (Action) list.get(i);
				if(temp.getName().equals(name))
					return i;
			}
			else if(list.get(i) instanceof Animation){
				Animation temp = (Animation) list.get(i);
				if(temp.getName().equals(name))
					return i;
			}
			else{
				if(name.equals(list.get(i)))
					return i;
			}
		}
		return -1;
	}

	public static <T> String[] toStringArray(ArrayList<T> list){
		String[] ret = new String[list.size()];
		for(int i = 0; i < list.size(); i++){
			if(list.get(i) instanceof Equip){
				ret[i] = ((Equip) list.get(i)).getName();
			}
			else if(list.get(i) instanceof Item){
				ret[i] = ((Item) list.get(i)).getName();
			}
			else if(list.get(i) instanceof Action){
				ret[i] = ((Action) list.get(i)).getName();
			}
		}
		return ret;
	}

	public static BufferedImage flip(BufferedImage bi){
		BufferedImage temp = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
		temp.getGraphics().drawImage(bi, 0, 0, temp.getWidth(), temp.getHeight(),
				bi.getWidth(), 0, 0, bi.getHeight(), null);
		return temp;
	}
	public static BufferedImage invert(BufferedImage currentImage) {
		BufferedImage ret = new BufferedImage(currentImage.getWidth(), currentImage.getHeight(), currentImage.getType());
		for(int i = 0; i < currentImage.getWidth(); i++){
			for(int j = 0; j < currentImage.getWidth(); j++){
				if(currentImage.getRGB(i, j) != 0){
					Color old = new Color(currentImage.getRGB(i, j));
					Color inv = new Color(255 - old.getRed(), 255 - old.getGreen(), 255 - old.getBlue());
					ret.setRGB(i, j, inv.getRGB());
				}
			}
		}
		return ret;
	}
}
